const path = require("path");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const webpack = require("webpack");

let pro = "production"
let mode;

const config = {
    entry: './static/js/IM/im.js',
    devServer: {
        open: true,
    },
    module: {
        rules: [
            {
                test: /\.html$/i,
                loader: 'html-loader',
            },
            {
                test: /\.css$/i,
                use: [MiniCssExtractPlugin.loader, 'css-loader'],
            },
        ],
    },
    plugins: [
        new CleanWebpackPlugin(),
        new MiniCssExtractPlugin({
            filename: "main.[hash].css",
            chunkFilename: '[id].css',
        }),
        new HtmlWebpackPlugin({
            template: "./index.html",
        })
    ],
    stats: 'minimal',
    optimization: {
        splitChunks: {
            chunks: 'async', //三选一："initial" 初始化，"all"(默认就是all)，"async"（动态加载） 
            minSize: 30000,  // 形成一个新代码块最小的体积,只有 >= minSize 的bundle会被拆分出来
            maxSize: 0, //拆分之前最大的数值，默认为0，即不做限制
            minChunks: 1, //引入次数，如果为2 那么一个资源最少被引用两次才可以被拆分出来
            maxAsyncRequests: 5,// 按需加载的最大并行请求数
            maxInitialRequests: 3, // 一个入口最大并行请求数
            automaticNameDelimiter: '~', // 文件名的连接符
            name: true,
            cacheGroups: {
                vendors: {
                    test: /[\\/]node_modules[\\/]/,
                    priority: -10
                },
                default: {
                    minChunks: 2,
                    priority: -20,
                    reuseExistingChunk: true
                }
            }
        }
    }
}



module.exports = (env, argv) => {



    if (argv.mode === 'development') {
        mode = 'development';
        config.mode = mode;
        config.output = {
            path: path.resolve(__dirname, "./test"),
            filename: 'test.[hash].js',
        }
    }

    if (argv.mode === 'production') {
        mode = 'production';
        config.mode = mode;
        config.output = {
            path: path.resolve(__dirname, './dist'),
            filename: 'bundle.[hash].js'
        }
        config.stats= {
            colors: true,
            usedExports: false,
            modules: false,
        }
    }


    return config;
};
